#!/usr/bin/env python

import os
import sys
import re
#from barchart import *
import numpy as np

def usage():
    print '''\
Usage: %s -s <stats>
''' % sys.argv[0]
    sys.exit(1)

def getopts(list, flags):
    import getopt
    try:
        opts, args = getopt.getopt(list, flags)
    except getopt.GetoptError:
        usage()

    return opts, args

result_dir = '/export/homeO5/pengfei/spec_workdir/results/trips/linux/microbench'
output_dir = '/export/homeO5/pengfei/spec_workdir/figs'
stats_file = '%s.stat'
variant = 'proc'
line_template = '%s\t%f'

#benchmarks = ['gzip','vpr','gcc','mcf',
#              'crafty','parser','vortex','bzip2','twolf',
#              'wupwise','swim','mgrid','applu',
#              'mesa','art','equake','ammp',
#              'apsi']

benchmarks = os.listdir(result_dir)
benchmarks.sort()
#conf1 = 'simple-normal-cache-double-fetch-hyperblock'
#conf1 = 'simple-fastMem-double-fetch'
#conf1 = 'simple-fastMem-double-fetch-hyperblock'
#conf2 = 'simple-fastMem-double-fetch-hyperblock'
#conf2 = 'simple-fastMem'

class VecStat(object):
    stat = np.zeros(5)
    refname = 'ref'
    def __init__(self,bname,sname,**kwargs):
        self.bname = bname
        self.sname = sname
        self.refname = rname
        stats_file = result_dir + '/' + bname + '/' + config + '/'  +  'stats.txt'
	for line in open(stats_file):
	    l = line.split()
	    if not l:
	        continue
	    if l[0] == bname and l[1] == sname:
	        for i, stat in enumerate(l):
	            if i > 1:
	                if i > 2:
	                    self.stat[i-2] =float( l[i])-float(l[i-1])
	                else:
	                    self.stat[i-2] = float(l[i])
		break
    
    def data(self):
        return self.stat

    def refname(self):
        return self.refname

class RefStat(object):
    stat = float(0)
    refname = 'ref'
    def __init__(self,bname,sname,rname,**kwargs):
        self.bname = bname
        self.sname = sname
        self.refname = rname
        stats_file = '%s.txt' % rname
	for line in open(stats_file):
	    l = line.split()
	    if not l:
	        continue
	    if l[0] == bname and l[1] == sname:
	        self.stat = float(l[2])
		break
    
    def data(self):
        return self.stat

    def refname(self):
        return self.refname
        
class Stat(object):
    stat = float(0)
    def __init__(self,bname,sname,variant,**kwargs):
        self.bname = bname
        self.sname = sname
        stats_file = result_dir + '/' + bname + '/' + variant + '/'  +  'tsim.stats'
	for line in open(stats_file):
	    l = line.split()
	    if not l:
	        continue
	    if l[0] == sname:
	        self.stat = float(l[1])
		break
    
    def data(self):
        return self.stat

class FunnyStat(object):
    stat = float(0)
    def __init__(self,bname,s1name,s2name,config,**kwargs):
        self.bname = bname
        self.s1name = s1name
        self.s2name = s2name
        stats_file = result_dir + '/' + bname + '/' + config + '/'  +  'stats.txt'
	for line in open(stats_file):
	    l = line.split()
	    if not l:
	        continue
	    if l[0] == s1name and l[1] == s2name:
	        self.stat = float(l[2])
		break
    
    def data(self):
        return self.stat

class Options:pass

if __name__ == '__main__':
    import getpass

    options = Options()
    options.stats = None
   
    opts, args = getopts(sys.argv[1:],'-s:')

    for o,a in opts:
        if o == '-s':
            options.stats = a.split(',')

    if not options.stats:
        sys.exit('Stats must be provided by -s')

    data = np.zeros(len(benchmarks)*1)
    
    data.shape = (len(benchmarks),1)

    for stat in options.stats:
        stats_file = stats_file % stat
        output_file = file(output_dir + '/' + stats_file, 'w')
        for i,bench in enumerate(benchmarks):
            s = Stat(bench,stat,'proc')
            data = s.data()
	    print data
            line = line_template % (bench,data) 
            print >>output_file,line 
        output_file.close()

    
